<?php
require_once("Response.php");
class Notice{
	public $link;
	public $response;
	public function __construct(){
		$this->response = new Response();
		$this->link = create_connection();
	}
	public function add_notice(){
		if(isset($_POST['data'])){
			$data = $_POST['data'];
			$admin_id = filter_sql($data['admin_id']);
			$title = filter_sql($data['title']);
			$content = filter_sql($data['content']);
			$addtime = filter_sql($data['addtime']);
			$is_top = $data['is_top']?filter_sql($data['is_top']):0;
			$qtype_id = filter_sql($data['qtype_id']);
			$importance = filter_sql($data['importance']);
			$status = filter_sql($data['status']);
			$insert = "insert into notice(admin_id,title,content,addtime,is_top,qtype_id,importance,status,look)
			values($admin_id,'$title','$content','$addtime',$is_top,$qtype_id,$importance,'$status',0);";
			// echo $insert;
			if($result = @execute_sql($this->link,'zhihu',$insert)){
				$this->response->returnData(200,'success',null);
			}else{
				$this->response->returnError();
			}
		}else {
			$this->response->returnError();
		}
	}

	public function get_notices(){
		$innersql = "select id from notice";
		$condition = "";
		// var_dump($_POST['data']);
		$sort = null;
		if(isset($_POST['data'])){
			$data = $_POST['data'];
			$page = intval(filter_sql($data['page']));
			$limit = intval(filter_sql($data['limit']));
			$importance = filter_sql($data['importance']);
			$title = filter_sql($data['title']);
			$type = filter_sql($data['type']);
			$sort = filter_sql($data['sort']);
			$inner = [];
			if($type) $inner[] = " qtype_id = $type";
			if($importance) $inner[] = " importance = $importance";
			if($title != null) $inner[] = " title like '%$title%'";
			$arrlength=count($inner);
			if($arrlength) $condition .= ' where';
			// var_dump($inner);
			for($x=0;$x<$arrlength-1;$x++)
				$condition .= $inner[$x] . ' and';
			if($arrlength>=1) $condition .= $inner[$arrlength-1];
			$condition .= " order by id";
			if($sort == '-id') $condition .= " desc";
			$innersql .= $condition;
			if($page && $limit) $innersql = $innersql . " limit ".($limit * ($page - 1)).",$limit";
		}else{
			$innersql .= " order by id limit 0,20 ";
		}
		// echo $innersql;
		$sql = "select count(id) as total from notice" . $condition;
		$result=@execute_sql($this->link,'zhihu',$sql);
		if($result) {
			$total = intval(mysqli_fetch_assoc($result)['total']);
			$sql = "select n.id as id,a.usernm as admin_name,title,
				content,is_top,look,addtime,importance,status,qtype_id
				from notice n 
                inner join ($innersql) as temp_table using(id) 
				left join admin a on n.admin_id = a.id order by n.id";
			if($sort == '-id') $sql .= " desc";
			$result=@execute_sql($this->link,'zhihu',$sql);
			if($result){
				$data = array();//初始化数组;
			    while ($row = mysqli_fetch_assoc($result)) {
			    	$alter = new NoticesData();//实例化对象;
					$alter->id = $row['id'];
					$alter->admin_name = $row['admin_name'];
					$alter->qtype_id = $row['qtype_id'];
					$alter->title = $row['title'];
					$alter->content = $row['content'];
					$alter->is_top = $row['is_top'];
					$alter->addtime = $row['addtime'];
					$alter->look = $row['look'];
					$alter->importance = $row['importance'];
					$alter->status = $row['status'];
					$data[] = $alter;
				}
				$this->response->returnData(200,'success',["items"=>$data,"total"=>$total]);
			}else {
				$this->response->returnError();
			}
		} else {
			$this->response->returnError();
		}
	}

	public function delete_notice(){
		if(isset($_POST['data'])){
			$data = $_POST['data'];
			$id = filter_sql($data['id']);
			$sql = "delete from notice where id=$id";
			// echo $sql;
			if($result = @execute_sql($this->link,'zhihu',$sql)){
				$this->response->returnData(200,'success',null);
			}else{
				$this->response->returnError();
			}
		}else {
			$this->response->returnError();
		}
	}

	public function update_notice(){
		if(isset($_POST['data'])){
			$data = $_POST['data'];
			$id = filter_sql($data['id']);
			$admin_id = filter_sql($data['admin_id']);
			$title = filter_sql($data['title']);
			$content = filter_sql($data['content']);
			$addtime = filter_sql($data['addtime']);
			$is_top = $data['is_top']?filter_sql($data['is_top']):0;
			$qtype_id = filter_sql($data['qtype_id']);
			$importance = filter_sql($data['importance']);
			$status = filter_sql($data['status']);
			$look = filter_sql($data['look']);
			$sql = "update notice set admin_id=$admin_id,title='$title',content='$content',addtime='$addtime',is_top=$is_top,qtype_id=$qtype_id,importance=$importance,status='$status',look=$look where id=$id";
			// echo $sql;
			if($result = @execute_sql($this->link,'zhihu',$sql)){
				$this->response->returnData(200,'success',null);
			}else{
				$this->response->returnError();
			}
		}else {
			$this->response->returnError();
		}
	}
}
class NoticesData
{
	public $id;
	public $admin_name;
	public $qtype_id;
	public $title;
	public $content;
	public $is_top;
	public $addtime;
	public $look;
	public $importance;
	public $status;
}